{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "precious-projection",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mBook\u001b[39m"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "case class Book(isbn: String)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "going-serbia",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mbook\u001b[39m: \u001b[32mBook\u001b[39m = \u001b[33mBook\u001b[39m(\u001b[32m\"xxx\"\u001b[39m)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val book = Book(\"xxx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "spanish-library",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mmassage\u001b[39m\r\n",
       "\u001b[36mmassage1\u001b[39m: \u001b[32mmassage\u001b[39m = \u001b[33mmassage\u001b[39m(\u001b[32m\"xxx\"\u001b[39m, \u001b[32m\"yyy\"\u001b[39m)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "case class massage (sender:String,body:String)\n",
    "val massage1 = massage(\"xxx\",\"yyy\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "public-starter",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xxx\n"
     ]
    }
   ],
   "source": [
    "println(massage1.sender)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "defined-touch",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "cmd4.sc:1: reassignment to val\n",
      "val res4 = massage1.body = \"111\"\n",
      "                         ^Compilation Failed"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Compilation Failed"
     ]
    }
   ],
   "source": [
    "massage1.body = \"111\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "clear-marker",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36ma\u001b[39m: \u001b[32mmassage\u001b[39m = \u001b[33mmassage\u001b[39m(\u001b[32m\"123\"\u001b[39m, \u001b[32m\"456\"\u001b[39m)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val a = massage(\"123\",\"456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "demanding-rochester",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mb\u001b[39m: \u001b[32mmassage\u001b[39m = \u001b[33mmassage\u001b[39m(\u001b[32m\"123\"\u001b[39m, \u001b[32m\"456\"\u001b[39m)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val b = massage(\"123\",\"456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "southern-headquarters",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mc\u001b[39m: \u001b[32mBoolean\u001b[39m = true"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val c = a == b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "responsible-relevance",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36md\u001b[39m: \u001b[32mmassage\u001b[39m = \u001b[33mmassage\u001b[39m(\u001b[32m\"789\"\u001b[39m, \u001b[32m\"456\"\u001b[39m)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val d = a.copy(sender=\"789\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "stupid-prompt",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[32mimport \u001b[39m\u001b[36mscala.util.Random\n",
       "\n",
       "\u001b[39m\r\n",
       "\u001b[36mx\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m9\u001b[39m"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "//模式匹配\n",
    "import scala.util.Random\n",
    "\n",
    "val x :Int = Random.nextInt(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "accepted-photography",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mres10\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"other\"\u001b[39m"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x match {\n",
    "    case 0 => \"zero\"\n",
    "    case _ => \"other\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ahead-feature",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36mmatchTest\u001b[39m\r\n",
       "\u001b[36mres11_1\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"er\"\u001b[39m"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def matchTest(x:Int) :String = x match {\n",
    "    case 1 => \"yi\"\n",
    "    case 2 => \"er\"\n",
    "    case _ => \"other\"\n",
    "}\n",
    "\n",
    "matchTest(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "exterior-western",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mres12\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"other\"\u001b[39m"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matchTest(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "chinese-boost",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mNotification\u001b[39m"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abstract class Notification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "electric-france",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mEmail\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mSMS\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mVoice\u001b[39m"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "case class Email (sender:String,title:String,body:String) extends Notification\n",
    "case class SMS (caller :String,massage :String) extends Notification\n",
    "case class Voice (contactName:String,link:String) extends Notification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "bigger-queen",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You got an SMS from 12345! Message: Are you there?\n",
      "you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36mshowNotification\u001b[39m\r\n",
       "\u001b[36msomeSms\u001b[39m: \u001b[32mSMS\u001b[39m = \u001b[33mSMS\u001b[39m(\u001b[32m\"12345\"\u001b[39m, \u001b[32m\"Are you there?\"\u001b[39m)\r\n",
       "\u001b[36msomeVoiceRecording\u001b[39m: \u001b[32mVoice\u001b[39m = \u001b[33mVoice\u001b[39m(\u001b[32m\"Tom\"\u001b[39m, \u001b[32m\"voicerecording.org/id/123\"\u001b[39m)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def showNotification(notification: Notification): String = notification match{\n",
    "   \n",
    "    case Email(sender, title, _) =>\n",
    "      s\"You got an email from $sender with title: $title\"\n",
    "    case SMS(number, message) =>\n",
    "      s\"You got an SMS from $number! Message: $message\"\n",
    "    case Voice(name, link) =>\n",
    "      s\"you received a Voice Recording from $name! Click the link to hear it: $link\"\n",
    "  \n",
    "}\n",
    "val someSms = SMS(\"12345\", \"Are you there?\")\n",
    "val someVoiceRecording = Voice(\"Tom\", \"voicerecording.org/id/123\")\n",
    "\n",
    "println(showNotification(someSms))  // prints You got an SMS from 12345! Message: Are you there?\n",
    "\n",
    "println(showNotification(someVoiceRecording))  // you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "governing-receptor",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You got an SMS from special someone!\n",
      "you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123\n",
      "You got an email from special someone!\n",
      "You got an SMS from special someone!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36mshowImportantNotification\u001b[39m\r\n",
       "\u001b[36mimportantPeopleInfo\u001b[39m: \u001b[32mSeq\u001b[39m[\u001b[32mString\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m\"867-5309\"\u001b[39m, \u001b[32m\"jenny@gmail.com\"\u001b[39m)\r\n",
       "\u001b[36msomeSms\u001b[39m: \u001b[32mSMS\u001b[39m = \u001b[33mSMS\u001b[39m(\u001b[32m\"867-5309\"\u001b[39m, \u001b[32m\"Are you there?\"\u001b[39m)\r\n",
       "\u001b[36msomeVoiceRecording\u001b[39m: \u001b[32mVoice\u001b[39m = \u001b[33mVoice\u001b[39m(\u001b[32m\"Tom\"\u001b[39m, \u001b[32m\"voicerecording.org/id/123\"\u001b[39m)\r\n",
       "\u001b[36mimportantEmail\u001b[39m: \u001b[32mEmail\u001b[39m = \u001b[33mEmail\u001b[39m(\n",
       "  \u001b[32m\"jenny@gmail.com\"\u001b[39m,\n",
       "  \u001b[32m\"Drinks tonight?\"\u001b[39m,\n",
       "  \u001b[32m\"I'm free after 5!\"\u001b[39m\n",
       ")\r\n",
       "\u001b[36mimportantSms\u001b[39m: \u001b[32mSMS\u001b[39m = \u001b[33mSMS\u001b[39m(\u001b[32m\"867-5309\"\u001b[39m, \u001b[32m\"I'm here! Where are you?\"\u001b[39m)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def showImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]): String = {\n",
    "  notification match {\n",
    "    case Email(sender, _, _) if importantPeopleInfo.contains(sender) =>\n",
    "      \"You got an email from special someone!\"\n",
    "    case SMS(number, _) if importantPeopleInfo.contains(number) =>\n",
    "      \"You got an SMS from special someone!\"\n",
    "    case other =>\n",
    "      showNotification(other) // nothing special, delegate to our original showNotification function\n",
    "  }\n",
    "}\n",
    "\n",
    "val importantPeopleInfo = Seq(\"867-5309\", \"jenny@gmail.com\")\n",
    "\n",
    "val someSms = SMS(\"867-5309\", \"Are you there?\")\n",
    "val someVoiceRecording = Voice(\"Tom\", \"voicerecording.org/id/123\")\n",
    "val importantEmail = Email(\"jenny@gmail.com\", \"Drinks tonight?\", \"I'm free after 5!\")\n",
    "val importantSms = SMS(\"867-5309\", \"I'm here! Where are you?\")\n",
    "\n",
    "println(showImportantNotification(someSms, importantPeopleInfo))\n",
    "println(showImportantNotification(someVoiceRecording, importantPeopleInfo))\n",
    "println(showImportantNotification(importantEmail, importantPeopleInfo))\n",
    "println(showImportantNotification(importantSms, importantPeopleInfo))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "prime-turtle",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Turning screen off\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mDevice\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mPhone\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mComputer\u001b[39m\r\n",
       "defined \u001b[32mfunction\u001b[39m \u001b[36mgoIdle\u001b[39m\r\n",
       "\u001b[36mabc\u001b[39m: \u001b[32mPhone\u001b[39m = \u001b[33mPhone\u001b[39m(\u001b[32m\"111\"\u001b[39m)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abstract class Device\n",
    "case class Phone(model: String) extends Device {\n",
    "  def screenOff = \"Turning screen off\"\n",
    "}\n",
    "case class Computer(model: String) extends Device {\n",
    "  def screenSaverOn = \"Turning screen saver on...\"\n",
    "}\n",
    "\n",
    "def goIdle(device: Device) = device match {\n",
    "  case p: Phone => p.screenOff\n",
    "  case c: Computer => c.screenSaverOn\n",
    "}\n",
    "\n",
    "val abc = Phone(\"111\")\n",
    "println (goIdle(abc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "human-america",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mFurniture\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mCouch\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mChair\u001b[39m\r\n",
       "defined \u001b[32mfunction\u001b[39m \u001b[36mfindPlaceToSit\u001b[39m"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sealed abstract class Furniture//保证其子类型的定义必须在同一个文件中\n",
    "case class Couch() extends Furniture\n",
    "case class Chair() extends Furniture\n",
    "\n",
    "def findPlaceToSit(piece: Furniture): String = piece match {\n",
    "  case a: Couch => \"Lie on the couch\"\n",
    "  case b: Chair => \"Sit on the chair\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "heavy-subdivision",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
